using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;
using SIS.ENT;

/// <summary>
/// The EmployeeAddressTableDB class is responsible for interacting with the database to retrieve and store information
/// about EmployeeAddressTable objects.
/// </summary>
public class EmployeeAddressTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of EmployeeAddressTable from the underlying datasource.
/// </summary>
/// <param name="employeeAddressKey">The unique EmployeeAddressKey of the EmployeeAddressTable in the database.</param>
/// <returns>An EmployeeAddressTable when the EmployeeAddressKey was found in the database, or null otherwise.</returns>
public static EmployeeAddressTable GetItem(int employeeAddressKey)
{
EmployeeAddressTable myEmployeeAddressTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeAddressTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@employeeAddressKey", employeeAddressKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myEmployeeAddressTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myEmployeeAddressTable;
}
}

/// <summary>
/// Returns a list with EmployeeAddressTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeAddressTable objects.</returns>
public static EmployeeAddressTableList GetList()
{
EmployeeAddressTableList tempList = new EmployeeAddressTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeAddressTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with EmployeeAddressTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeAddressTable objects.</returns>
public static EmployeeAddressTableList GetListByUserAccountKey(int UserAccountKey)
{
EmployeeAddressTableList tempList = new EmployeeAddressTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeAddressTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

public static EmployeeAddressTable GetItemByUserAccountKey(int UserAccountKey, short AddressTypeCode)
{
    EmployeeAddressTable myEmployeeAddressTable = null;
    using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
    {
        using (SqlCommand myCommand = new SqlCommand("sprocEmployeeAddressTableSelectSingleItemByUserAccountKey", myConnection))
        {
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue("@UserAccountKey", UserAccountKey);
            myCommand.Parameters.AddWithValue("@AddressTypeCode", AddressTypeCode);
            myConnection.Open();
            using (SqlDataReader myReader = myCommand.ExecuteReader())
            {
                if (myReader.Read())
                {
                    myEmployeeAddressTable = FillDataRecord(myReader);
                }
                myReader.Close();
            }
            myConnection.Close();
        }
        return myEmployeeAddressTable;
    }
}

/// <summary>
/// Saves a EmployeeAddressTable in the database.
/// </summary>
/// <param name="myEmployeeAddressTable">The EmployeeAddressTable instance to save.</param>
/// <returns>The new EmployeeAddressKey if the EmployeeAddressTable is new in the database or the existing EmployeeAddressKey when an item was updated.</returns>
public static int Save(EmployeeAddressTable myEmployeeAddressTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeAddressTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myEmployeeAddressTable.EmployeeAddressKey == -1)
{myCommand.Parameters.AddWithValue("@employeeAddressKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@employeeAddressKey", myEmployeeAddressTable.EmployeeAddressKey);
}
if (myEmployeeAddressTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myEmployeeAddressTable.UserAccountKey);
}
if (myEmployeeAddressTable.UnitNo == null){
myCommand.Parameters.AddWithValue("@unitNo", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@unitNo", myEmployeeAddressTable.UnitNo);
}
if (myEmployeeAddressTable.LevelNo == null){
myCommand.Parameters.AddWithValue("@levelNo", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@levelNo", myEmployeeAddressTable.LevelNo);
}
if (myEmployeeAddressTable.StreetName == null){
myCommand.Parameters.AddWithValue("@streetName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@streetName", myEmployeeAddressTable.StreetName);
}
if (myEmployeeAddressTable.TownshipCode == null){
myCommand.Parameters.AddWithValue("@townshipCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@townshipCode", myEmployeeAddressTable.TownshipCode);
}
if (myEmployeeAddressTable.CityCode == null){
myCommand.Parameters.AddWithValue("@cityCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@cityCode", myEmployeeAddressTable.CityCode);
}
if (myEmployeeAddressTable.DistrictCode == null){
myCommand.Parameters.AddWithValue("@districtCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@districtCode", myEmployeeAddressTable.DistrictCode);
}
if (myEmployeeAddressTable.CountryCode == null){
myCommand.Parameters.AddWithValue("@countryCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@countryCode", myEmployeeAddressTable.CountryCode);
}
if (myEmployeeAddressTable.PostalCode == null){
myCommand.Parameters.AddWithValue("@postalCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@postalCode", myEmployeeAddressTable.PostalCode);
}
if (myEmployeeAddressTable.AddressTypeCode == null){
myCommand.Parameters.AddWithValue("@addressTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@addressTypeCode", myEmployeeAddressTable.AddressTypeCode);
}
if (myEmployeeAddressTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myEmployeeAddressTable.Status);
}
if (myEmployeeAddressTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myEmployeeAddressTable.CreatedDateTime);
}
if (myEmployeeAddressTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myEmployeeAddressTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a EmployeeAddressTable from the database.
/// </summary>
/// <param name="employeeAddressKey">The EmployeeAddressKey of the EmployeeAddressTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int employeeAddressKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeAddressTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@employeeAddressKey", employeeAddressKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the EmployeeAddressTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static EmployeeAddressTable FillDataRecord(IDataRecord myDataRecord )
{
EmployeeAddressTable myEmployeeAddressTable = new EmployeeAddressTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmployeeAddressKey")))
{
myEmployeeAddressTable.EmployeeAddressKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("EmployeeAddressKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myEmployeeAddressTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UnitNo")))
{
myEmployeeAddressTable.UnitNo = myDataRecord.GetString(myDataRecord.GetOrdinal("UnitNo"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LevelNo")))
{
myEmployeeAddressTable.LevelNo = myDataRecord.GetString(myDataRecord.GetOrdinal("LevelNo"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StreetName")))
{
myEmployeeAddressTable.StreetName = myDataRecord.GetString(myDataRecord.GetOrdinal("StreetName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TownshipCode")))
{
myEmployeeAddressTable.TownshipCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("TownshipCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CityCode")))
{
myEmployeeAddressTable.CityCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CityCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DistrictCode")))
{
myEmployeeAddressTable.DistrictCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DistrictCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CountryCode")))
{
myEmployeeAddressTable.CountryCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CountryCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PostalCode")))
{
myEmployeeAddressTable.PostalCode = myDataRecord.GetString(myDataRecord.GetOrdinal("PostalCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AddressTypeCode")))
{
myEmployeeAddressTable.AddressTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("AddressTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myEmployeeAddressTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
    myEmployeeAddressTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myEmployeeAddressTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myEmployeeAddressTable;
}



}
